import { getShoppingCartInfo,getInfoByIdApp,
  saveShoppingCart,getCount,
  addShoppingCart,deleteShoppingCart,getBusinessReceiveStatus } from '../services/shoppingCart'

export default {

  namespace: 'shoppingCart',

  state: {
    shoppingCarts:[],
    deliveryFee:{},
    cartCount:0,
    isInTime:true,
    isBack:false
  },



  effects: {
    *addShoppingCart({ payload }, { call, put }) {  // 添加购物车
      yield call(addShoppingCart,payload.addQ)
      const resp = yield call(getCount,payload.countQ)
      yield put(
        { type: 'selSCart' ,
          state: {
            cartCount: resp.data.data.countNum,
          }
        }
      );
    },
    *selShoppingCartCount({ payload }, { call, put }) {  // 查询购物车数量
      const resp = yield call(getCount,payload)
      yield put(
        { type: 'selSCart' ,
          state: {
            cartCount: resp.data.data.countNum,
          }
        }
      );
    },
    *selShoppingCart({ payload }, { call, put }) {  // 查询购物车详情
      const resp = yield call(getShoppingCartInfo,payload)
      yield put(
        { type: 'selSCart' ,
          state: {
            shoppingCarts: resp.data.data.commodityLists,
          }
        }
      );
    },
    *selShoppingCartAndFee({ payload }, { call, put }) {  // 查询购物车及包邮值
      const resp1 = yield call(getShoppingCartInfo,payload)
      const resp2 = yield call(getInfoByIdApp,payload)
      const resp3 = yield call(getBusinessReceiveStatus,payload)
      yield put(
        { type: 'selSCart' ,
          state: {
            shoppingCarts: resp1.data.data.commodityLists,
            deliveryFee: resp2.data.data,
            isInTime:resp3.data.data
          }
        }
      );
    },
    *saveShoppingCart({ payload }, { call, put }) {  // 编辑购物车--保存商品
      yield call(saveShoppingCart,payload.cart)
      const resp = yield call(getShoppingCartInfo,payload.qs)
      yield put(
        { type: 'selSCart' ,
          state: {
            shoppingCarts: resp.data.data.commodityLists,
            isBack:true
          }
        }
      );
    },
    *delShoppingCart({ payload }, { call, put }) {  // 编辑购物车--删除商品
      yield call(deleteShoppingCart,payload.cart)
      const resp = yield call(getShoppingCartInfo,payload.qs)
      yield put(
        { type: 'selSCart' ,
          state: {
            shoppingCarts: resp.data.data.commodityLists,
          }
        }
      );
    },
  },

  reducers: {
    selSCart(state, action) {
      return { ...state, ...action.state };
    },
  },

};
